<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id_user
 * @property integer $id_company
 * @property integer $id_group
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $address
 * @property string $phone
 * @property integer $is_admin
 * @property string $updated
 * @property string $created
 *
 * @property Company $idCompany
 */
class User extends \yii\db\ActiveRecord
{
    const TYPE_ADMIN = 1;
    const TYPE_USER = 0;
    


    public $password_input;
    public $password_repeat;
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['password_input', 'password_repeat'], 'required','on' => 'creating'],
            ['password_repeat', 'compare', 'compareAttribute' => 'password_input','on' => 'creating'],
            [['name', 'address', 'email', 'phone'], 'required'],
            [['id_user', 'id_company', 'id_group', 'is_admin'], 'integer'],
            ['email', 'email'],
            ['email', 'unique'],
            [['updated', 'created'], 'safe'],
            [['name', 'email', 'password', 'address', 'phone'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id_user' => 'Id User',
            'id_company' => 'Id Company',
            'id_group' => 'Id Group',
            'name' => 'Họ và tên',
            'email' => 'Email',
            'password' => 'Mật khẩu',
            'address' => 'Địa chỉ',
            'phone' => 'Số dt',
            'password_input' => 'Mật khẩu',
            'password_repeat' => 'Xác nhận',
            'is_admin' => 'Is Admin',
            'updated' => 'Updated',
            'created' => 'Created',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getIdCompany()
    {
        return $this->hasOne(Company::className(), ['id_company' => 'id_company']);
    }
    
    /**
     * 
     */
    public function beforeSave($insert) {
        parent::beforeSave($insert);
        if($insert){
            $this->created = date('Y-m-d');
        }
        $this->updated = date('Y-m-d');
        return true;
    }
    
    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password = Yii::$app->security->generatePasswordHash($password);
    }
    
    /**
     * 
     */
    public function comparePassword($password){
        return Yii::$app->security->validatePassword($password, $this->password);
    }
    
    /**
     * 
     */
    public function findRootCategory(){
        $root = CategoryCompany::find()->andWhere(['id_company' => $this->id_company,'level' => 1])->one();
        if($root){
            return $root->id_category_company;
        }
        return 0;
    }
    
    /**
     * 
     */
    public function findBrandForLogin() {
        $return = [
            'main' => 0,
            'brands' => []
        ];
        $brands = Branch::find()->andWhere(['id_company' => $this->id_company])->all();
        if($brands){
            foreach($brands as $brand){
                $return['brands'][] = [
                    'id_branch' => $brand->id_branch,
                    'name' => $brand->name,
                ];
                if($brand->is_main){
                    $return['main'] = $brand->id_branch;
                }
            }
        }
        return $return;
    }
}
